import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisStreamCommands;
import org.junit.Before;
import org.junit.Test;

public class Redis {
    private RedisClient redisClient;
    private StatefulRedisConnection<String,String> connect;
    @Before
    public void beforeTest(){
        redisClient=RedisClient.create("redis://localhost");
        System.out.println("init");
    }

    @Test
    public void string(){
        connect=redisClient.connect();
        //增
       connect.sync().set("name","tian");
        //key 值相同则实现修改的功能
        connect.sync().set("name","titi");
        //获取数据get用key取value值
        String value=connect.sync().get("name");
        System.out.println(value);
        //删除
        connect.sync().del("name");
        //关掉客户端和连接
        connect.close();
        redisClient.shutdown();
    }
    @Test
    public void hash(){
        connect=redisClient.connect();
        //增
        connect.sync().hset("name","a1","aa");
        connect.sync().hset("name","b1","bb");
        //获取key中全部value
        System.out.println(connect.sync().hgetall("name"));
        //删除
        connect.sync().del("name","a1");

        connect.close();
        redisClient.shutdown();
    }

    @Test
    public void list(){
        connect=redisClient.connect();
        //增
        //左边添加
        connect.sync().lpush("list","1","2","3");
        //右边添加
        connect.sync().rpush("list1","1","2","3");
        //左边读取
        System.out.println(connect.sync().lrange("list",0,2));
        System.out.println(connect.sync().lrange("list1",0,2));

        //lpop　从链表的最左边删除一位数据
        //rpop 从链表的最右边删除一位数据
        connect.sync().lpop("list");
        System.out.println(connect.sync().lrange("list",0,1));
        //截取范围内值：ltrim
        System.out.println(connect.sync().ltrim("list1", 1, 2));

        connect.close();
        redisClient.shutdown();
    }

    @Test
    public void set(){
        /**
         *  String 的无序排列, 适合用于表示对象间的关系
         */
        connect=redisClient.connect();
        //增
        connect.sync().sadd("set","1","2","3","4");
        //随机删除里面的数据
        connect.sync().spop("set");
        //查询set数据，输出set里面全部的数据(无序的输入)
        System.out.println(connect.sync().smembers("set"));
        //在set里面查询特定元素,如果set里面有这个元素，则返回true　否则返回false
        System.out.println(connect.sync().sismember("set","3"));

        connect.close();
        redisClient.shutdown();
    }
    @Test
    public void sortedSet(){
        connect=redisClient.connect();
        //有序排序　
        connect.sync().zadd("sorted",1,"test1");
        connect.sync().zadd("sorted", 2, "test2");
        connect.sync().zadd("sorted", 1, "test3");
        connect.sync().zadd("sorted", 3, "test4");

        //获取sortedset集合数据的长度
        Long length=connect.sync().zcard("sorted");
        //输出数据　输出的数据会按照v值的大小排序(由小到大)
        System.out.println(connect.sync().zrange("sorted", 0,length));
        //删除指定元素
        connect.sync().zrem("sorted","test1");


        connect.close();
        redisClient.shutdown();
    }

}
